package es.ats.codington.web.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.services.PlaceService;

/**
 * Servlet implementation class ShowPlacesAdminServlet
 */
@WebServlet("/ShowPlacesAdminServlet")
public class ShowPlacesAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowPlacesAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		 
		HttpSession session = request.getSession(true);
		
		
		PlaceService service2 = new PlaceService();
		List<Place> places = new ArrayList<Place>();
		/**
		 * Get all places
		 */
		places = service2.getAllPlaces();
		
		session.setAttribute("placesList", places);
		
		/**
	     * Invokes the action "updateplacesadmin.jsp"
	     */
		RequestDispatcher rd = request.getRequestDispatcher("updateplacesadmin.jsp");
		rd.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int idPlace = 0;
		response.setContentType("text/html");
        
		HttpSession session = request.getSession(true);
		
		/**
		 * Get the id of event
		 */
		idPlace = Integer.parseInt(request.getParameter("idPlaceAdmin"));
		session.setAttribute("idPlaceAdminSession", idPlace);
		
		/**
	     * Invokes the action "updateevent.jsp"
	     */
		response.sendRedirect("updateplace.jsp");	
	}

}
